package com.buyouchina.bean.product;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import com.buyouchina.bean.area.Province;

@Entity
@Table(name = "tongji")
public class Statistical {

	@Id
	@GeneratedValue
	private Integer id;

	// 分类
	private String category;

	// 数量
	private Double quantity;

	// 操作类型(买入&卖出)
	@Enumerated(EnumType.STRING)
	@Column(length = 20)
	private StatisticalType type;

	// 统计级别(市级别&县级别)
	@Enumerated(EnumType.STRING)
	@Column(length = 20)
	private TongjiAreaType areaType;

	// 成交订单总数量
	private Integer totalOrder;

	// 今日成交订单数量
	private Integer todayOrder;

	// 时间
	@Temporal(TemporalType.TIMESTAMP)
	private Date date;

	// 货物
	@ManyToOne(fetch = FetchType.EAGER, cascade = { CascadeType.REFRESH })
	@JoinColumn(name = "product_id")
	private Product product;

	// 收货店铺
	@ManyToOne(fetch = FetchType.EAGER, cascade = { CascadeType.REFRESH })
	@JoinColumn(name = "to_id")
	private Outlets to;

	// 发货店铺
	@ManyToOne(fetch = FetchType.EAGER, cascade = { CascadeType.REFRESH })
	@JoinColumn(name = "from_id")
	private Outlets from;

	//	统计记录的区域
	@ManyToOne(fetch = FetchType.EAGER, cascade = { CascadeType.REFRESH })
	@JoinColumn(name = "province_id")
	private Province province;
	
	//	次统计记录区域的订单
	@ManyToMany(fetch = FetchType.EAGER, cascade = { CascadeType.REFRESH })
	@JoinTable(name = "tongji_orders", 
			joinColumns = { @JoinColumn(name = "tongji_id", referencedColumnName = "id", nullable = true) }, 
			inverseJoinColumns = { @JoinColumn(name = "order_id", referencedColumnName = "id", nullable = true) })
	private Set<OrderForm> orders=new HashSet<OrderForm>(); 

	public enum StatisticalType {
		SALES {
			public String getName() {
				return "销售统计";
			}
		},
		PURCHASE {
			public String getName() {
				return "进货统计";
			}
		};
		public abstract String getName();
	}

	public enum TongjiAreaType {
		CITYLEVEL {
			public String getName() {
				return "市级";
			}
		},
		COUNTYLEVEL {
			public String getName() {
				return "省级";
			}
		},
		STATELEVEL {
			public String getName() {
				return "国家级";
			}
		};
		public abstract String getName();
	}

	public Statistical() {
		this.date = new Date();
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public Integer getTotalOrder() {
		return totalOrder;
	}

	public void setTotalOrder(Integer totalOrder) {
		this.totalOrder = totalOrder;
	}

	public Integer getTodayOrder() {
		return todayOrder;
	}

	public void setTodayOrder(Integer todayOrder) {
		this.todayOrder = todayOrder;
	}

	public Province getProvince() {
		return province;
	}

	public void setProvince(Province province) {
		this.province = province;
	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public Outlets getTo() {
		return to;
	}

	public void setTo(Outlets to) {
		this.to = to;
	}

	public Outlets getFrom() {
		return from;
	}

	public void setFrom(Outlets from) {
		this.from = from;
	}

	public Double getQuantity() {
		return quantity;
	}

	public void setQuantity(Double quantity) {
		this.quantity = quantity;
	}

	public void setType(StatisticalType type) {
		this.type = type;
	}

	public StatisticalType getType() {
		return type;
	}

	public TongjiAreaType getAreaType() {
		return areaType;
	}

	public void setAreaType(TongjiAreaType areaType) {
		this.areaType = areaType;
	}

	public Set<OrderForm> getOrders() {
		return orders;
	}

	public void setOrders(Set<OrderForm> orders) {
		this.orders = orders;
	}

}
